//
//  ChangePassWordViewController.m
//  CYWFrame
//
//  Created by Macx on 2017/9/29.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "ChangePassWordViewController.h"
#import "NSString+Common.h"

@interface ChangePassWordViewController () <UITextFieldDelegate>

@end

@implementation ChangePassWordViewController {
	UITextField *_oldPassWord;
	UITextField *_newPassWord;
}

- (void)viewDidLoad {
	[super viewDidLoad];
	// Do any additional setup after loading the view.
	
	
	[self buildUI];
	
}

#pragma mark ==========构建UI==========
- (void)buildUI {
	
	self.navigationItem.title = @"修改密码";
	self.view.backgroundColor = kWhiteColor;
	
	NSArray *arr = @[@"旧 密 码：", @"新 密 码："];
	NSArray *placeHolder = @[@"请点击输入旧密码",@"请点击输入新密码"];
	CGSize size = [LZCTools textSizeWithText:@"测字数试试" font:kUHSystemFontWithSize(14)];
	for (int i = 0; i < arr.count; i++) {
		
		UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, SW(8)+SW(8)*i, size.width, size.height)];
		[self.view addSubview:label];
		label.textColor = kGrayColor;
		label.textAlignment = NSTextAlignmentCenter;
		label.text = arr[i];
		label.font = kUHSystemFontWithSize(14);
		
		UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(30+size.width+10, SW(8)+SW(8)*i, SCREEN_WIDTH*2/3, size.height)];
		[self.view addSubview:field];
		field.delegate = self;
		field.tag = 11+i;
		field.textColor = kGrayColor;
		field.clearButtonMode = UITextFieldViewModeWhileEditing;
		field.font = kUHSystemFontWithSize(14);
		field.placeholder = placeHolder[i];
		if (i == 1) {
			field.secureTextEntry = YES;
			_newPassWord = field;
		}
		else
			_oldPassWord = field;
		
		UIView *line = [[UIView alloc] initWithFrame:CGRectMake(20, SW(8)+size.height+5+SW(8)*i, SCREEN_WIDTH-40, 1)];
		[self.view addSubview:line];
		line.backgroundColor = kLightGrayColor;
		line.tag = 21+i;
		
	}
	
	
	UIView *line2 = [self.view viewWithTag:22];
	
	UIButton *completeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
	[self.view addSubview:completeBtn];
	[completeBtn setTitle:@"完成" forState:UIControlStateNormal];
	[completeBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
	CGFloat btnHeight = [LZCTools textSizeWithText:@"字数" font:kUHSystemFontWithSize(14)].height+SW(20);
	completeBtn.frame = CGRectMake(20, line2.y+1+20, SCREEN_WIDTH-40, btnHeight);
	completeBtn.backgroundColor = UIMainColorRGB;
	completeBtn.layer.cornerRadius = 5;
	completeBtn.layer.masksToBounds = YES;
	[completeBtn addTarget:self action:@selector(completeClick:) forControlEvents:UIControlEventTouchUpInside];
	
}

#pragma mark ===========响应方法==========
/**
 完成密码响应
 */
- (void)completeClick:(id)sender {
	NSLog(@"完成新密码响应");
	
	// 密码不为空
	if ([NSString isEmpty:_newPassWord.text] || [NSString isEmpty:_oldPassWord.text]) {
		[self presentViewController:[LZCTools alertMessage:@"密码不可为空"] animated:YES completion:nil];
		return;
	}
	// 密码长度要大于6
	if (_oldPassWord.text.length < 6 || _newPassWord.text.length < 6) {
		[self presentViewController:[LZCTools alertMessage:@"密码不可以少于6位数"] animated:YES completion:nil];
		return;
	}
	NSDictionary *param = @{@"old_password":_oldPassWord.text,
							@"new_password":_newPassWord.text};
	
	[[NetworkTool sharedTool] requestWithName:@"修改密码" URLString:APIResetpasswords parameters:param method:@"PUT" callBack:^(id responseObject) {
		[self.navigationController popViewControllerAnimated:YES];
	}];
	
}

#pragma mark ===========代理方法==========
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
	[textField resignFirstResponder];
	return YES;
}


- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
	// Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
